package com.dongdongshop.shiro;

import com.dongdongshop.ex.ShopLoginException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.ShopLoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;


public class LoginRealm extends AuthorizingRealm {

    @DubboReference
    private ShopLoginService shopLoginService;

    //授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("进入受权方法");
        return null;
    }

    //认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("进入登陆方法");
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String sellerId =token.getUsername();
        TbSeller tbSeller =shopLoginService.selectByname(sellerId);
        //判断账号是否存在
        if (tbSeller == null){
            return null;
        }
        //判断是否已审核
        if (!tbSeller.getStatus().equals("1")){
            throw new ShopLoginException("未审核");
        }
        //第一个是登录成功后需要放到session中的对象  第二个是数据库查出来的密码 第三个是 盐 第四个用户的名字(账号)
        return new SimpleAuthenticationInfo(tbSeller,tbSeller.getPassword(), ByteSource.Util.bytes(tbSeller.getSalt()), tbSeller.getSellerId());
    }
}
